import 'package:flutter/material.dart';

class FormDemoPage extends StatefulWidget {
  @override
  _FormDemoPageState createState() => _FormDemoPageState();
}

class _FormDemoPageState extends State<FormDemoPage> {
  List _hobby = [
    {
      'title': '吃饭',
      'checked': false
    },
    {
      'title': '睡觉',
      'checked': false
    },
    {
      'title': '写代码',
      'checked': false
    },
    {
      'title': '看电影',
      'checked': false
    }
  ];
  Map _userInfo = {
    'username': '',
    'sex': 1,
    'hoppy': new List()
  };
  List _hoppyList = new List();
  // 设置性别
  void _setSex (value) {
    setState(() {
      this._userInfo['sex'] = value;
    });
  }
  List<Widget> _getHoppy() {
    var list = this._hobby.map((item) {
      return Row(
        children: <Widget>[
          Text(item['title']),
          Checkbox(
            value: item['checked'],
            onChanged: (v) {
              setState(() {
                item['checked'] = v;
                if (v) {
                  this._hoppyList.add(item);
                } else {
                  this._hoppyList.remove(item);
                }
              });
            },
          ),

        ],
      );
    });
    return list.toList();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('formDemo'),
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.all(20),
          child: Column(
            children: <Widget>[
              TextField(
                decoration: InputDecoration(
                    hintText: '请输入姓名',
                    border: OutlineInputBorder()
                ),
                onChanged: (v) {
                  setState(() {
                    this._userInfo['username'] = v;
                  });
                },
              ),
              SizedBox(height: 20,),
              Row(
                children: <Widget>[
                  Text('男:'),
                  Radio(
                    value: 1,
                    groupValue: this._userInfo['sex'],
                    onChanged: this._setSex,
                  ),
                  Text('女:'),
                  Radio(
                    value: 2,
                    groupValue: this._userInfo['sex'],
                    onChanged: this._setSex,
                  )
                ],
              ),
              SizedBox(height: 20,),
              Column(
                children: this._getHoppy(),
              ),
              SizedBox(height: 30,),
              TextField(
                maxLines: 4,
                maxLength: 100,
                decoration: InputDecoration(
                    hintText: '输入描述',
                    border: OutlineInputBorder()
                ),
              ),
              SizedBox(height: 30,),
              RaisedButton(
                child: Text('提交'),
                color: Colors.blue,
                textColor: Colors.white,
                onPressed: () {
                  setState(() {
                    this._userInfo['hoppy'] = this._hoppyList;
                  });
                  print(this._userInfo);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
